//
//  AddadressVC.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/6/13.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "AddadressVC.h"
#import "UIViewController+KNSemiModal.h"
#import "ChooseLocationView.h"
#import "CitiesDataTool.h"

@interface AddadressVC ()<NSURLSessionDelegate,UIGestureRecognizerDelegate,CloseViewDelegate>
@property (nonatomic,strong) UIView  *cover;
@property (nonatomic,strong) ChooseLocationView *chooseLocationView;
@end

@implementation AddadressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [[CitiesDataTool sharedManager] requestGetData];
    self.chooseLocationView.frame = CGRectMake(0, 0, self.view.frame.size.width, 320);
    self.chooseLocationView.delegate = self;
    self.chooseLocationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.cover];//弹出的view
    
    [self.closeButton setImage:[UIImage imageNamed:@"close_btn"] forState:UIControlStateNormal];
    [self.closeButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0,-50)];
    
    self.cover.hidden = !self.cover.hidden;
    self.chooseLocationView.hidden = self.cover.hidden;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)closeButton:(id)sender
{
    [self closeViewController];
}

#pragma mark - CloseViewDelegate

- (void)closeViewButton:(NSString *)address
{
    [self closeViewController];
}

- (void)modifyNavigationframe
{
    self.view.frame = CGRectMake(0,APP_HEIGHT - 380, APP_WIDTH, 600);
}

- (void)returnKeyboardModifyNabFrame
{
    self.view.frame = CGRectMake(0,APP_HEIGHT - 350, APP_WIDTH, 600);

}

#pragma mark - 关闭弹出的半截视图
//弹出半截页面的页面回收方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self closeViewController];

}

- (void)closeViewController {
    
    UIViewController * parent = [self.view containingViewController];
    if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
        [parent dismissSemiModalView];
    }
    
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_chooseLocationView.frame, point)){
        return NO;
    }
    return YES;
}


- (void)tapCover:(UITapGestureRecognizer *)tap{
    
    if (_chooseLocationView.chooseFinish) {
        _chooseLocationView.chooseFinish();
    }
}


- (ChooseLocationView *)chooseLocationView{
    
    if (!_chooseLocationView) {
        _chooseLocationView = [[ChooseLocationView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 350, [UIScreen mainScreen].bounds.size.width, 350)];
        
    }
    return _chooseLocationView;
}

- (UIView *)cover{
    
    if (!_cover) {
        _cover = [[UIView alloc]initWithFrame:CGRectMake(0, 30, APP_WIDTH, 400)];
        _cover.backgroundColor = [UIColor blueColor];
        _cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        [_cover addSubview:self.chooseLocationView];
        __weak typeof (self) weakSelf = self;
        _chooseLocationView.chooseFinish = ^{
            [UIView animateWithDuration:0.25 animations:^{
//                weakSelf.label.text = weakSelf.chooseLocationView.address;
                weakSelf.view.transform = CGAffineTransformIdentity;
                weakSelf.cover.hidden = YES;
            }];
        };
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCover:)];
        [_cover addGestureRecognizer:tap];
        tap.delegate = self;
        _cover.hidden = YES;
    }
    return _cover;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
